package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;


    public void save(Notice notice,List<Employee> empIds) {
        //创建时间，创建人，已读/未读(默认未读)
        Date date = new Date();
        String format = DateUtil.format(date,"yyyy-MM-dd HH:mm:ss");
        notice.setCreateTime(date);
        notice.setEmployee(UserContext.getCurrentUser());
        //notice.setNoticeStatus(false);
        noticeMapper.insert(notice);
        //插入关联表关系
        //noticeMapper.insertEmpNoticeRelation(UserContext.getCurrentUser().getId(),notice.getId());
        noticeMapper.insertEmpNoticeRelation(empIds,notice.getId());

    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(QueryObject qo,Long employeeId) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectForList(qo,employeeId); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);
    }

    //修改公告状态:已发布与未发布
    public void updateNoticeStatus(Long id,Boolean status) {
        noticeMapper.updateNoticeStatus(id,status);
    }

    //修改消息状态
    public void updateMsgStatus(Long noticeId,Long employeeId) {
        noticeMapper.updateMsgStatus(noticeId,employeeId);
    }


}
